
import static org.junit.Assert.*;

import gameMechanics.Card;

import java.io.IOException;
import java.util.ArrayList;

import org.junit.Test;



public class ConnorTests {
	private Thread server;
	private TexasReceiver rec;
	private TexasSender ts;
	public void createListener(){
		final TexasReceiver receiver;
		try {
			receiver = new TexasReceiver(7380);
			receiver.setNotificationListener(new NotificationCenter(){

				@Override
				public void playerMadeMove(Turn t) {
					System.out.println(t);
					
				}

				@Override
				public void cardsReceived(HandCards h) {
					System.out.println(h);
					
				}
				
			});
			ConnorTests.this.rec = receiver;
			server = new Thread(){
				@Override
				public void run(){
					try {
						
						
						receiver.listen();
					} catch (IOException e) {
						System.out.println("Socket closed");
					}
				}
			};
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		server.start();
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	@Test
	public void test() {
		createListener();
		ts = new TexasSender();
		ArrayList<Card> table = new ArrayList<Card>();
		table.add(new Card(0));
		table.add(new Card(1));
		table.add(new Card(2));
		table.add(new Card(3));
		table.add(new Card(4));
		HandCards hc = new HandCards(table);
		hc.addPlayerHand(new PlayerHand(new Card(5), new Card(6)));
		hc.addPlayerHand(new PlayerHand(new Card(7), new Card(8)));
		rec.setMostRecent(null);
		ts.blastCards(hc);
		while (!rec.recentMessageReady()) {}
		rec.closeListener();
		assertTrue(rec.getMostRecent().equals("deal_cards:{{0}{1}{2}{3}{4}{{5}{6}}{{7}{8}}}"));
	}
	@Test
	public void test2(){
		createListener();
		if (ts == null)
			ts = new TexasSender();
		Turn t = new Turn(0,TurnAction.FOLD,0);
		rec.setMostRecent(null);
		ts.takeTurn(t);
		while (!rec.recentMessageReady()){}
		rec.closeListener();
		assertTrue(rec.getMostRecent().equals("turn:{0,FOLD,0}"));
	}
	@Test
	public void test3(){
		ArrayList<Card> table = new ArrayList<Card>();
		table.add(new Card(0));
		table.add(new Card(1));
		table.add(new Card(2));
		table.add(new Card(3));
		table.add(new Card(4));
		HandCards hc = new HandCards(table);
		hc.addPlayerHand(new PlayerHand(new Card(5), new Card(6)));
		hc.addPlayerHand(new PlayerHand(new Card(7), new Card(8)));
		assertTrue(hc.equals(new HandCards("{0}{1}{2}{3}{4}{{5}{6}}{{7}{8}}")));
	}
	@Test
	public void test4(){
		Turn t = new Turn(0,TurnAction.FOLD,0);
		assertTrue(t.equals(new Turn("0,FOLD,0")));
	}

}
